/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view.inventory;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import model.inventory.AdjDetail;
import utility.com.TextComponentUtils;

/**
 *
 * @author Yusup Jauhari Shandi <in.visi>
 */
public class AdjMstTM extends AbstractTableModel{
    private List<AdjDetail> data;
    private boolean readOnly;
    /*COL_NO COL_PLU COL_NAMA  COL_UNIT COL_STOK COL_QTY COL_SELISIH COL_HARGA COL_TOTADJ COL_TOTSELISIH   */
    static protected String[] columnNames = { "NO", "PLU", "NAMA","SATUAN","STOK","QTY","SELISIH","HPP","TOT.ADJ","TOT.SELISIH" };
    static protected Class<?>[] columnTypes = { Integer.class, String.class, String.class, String.class,BigDecimal.class,BigDecimal.class,BigDecimal.class,BigDecimal.class,BigDecimal.class,BigDecimal.class }; 
    
    public AdjMstTM() {
        this(new ArrayList<AdjDetail>(0));
    }

    public AdjMstTM(List<AdjDetail> data) {
        this.data = data;
    }

    public AdjMstTM(List<AdjDetail> data, boolean readOnly) {
        this.data = data;
        this.readOnly = readOnly;
    }
    
    
    
    
    public List<AdjDetail> getData() {
        return data;
    }

    public void setData(List<AdjDetail> data) {
        this.data = data;
    }
    
    public AdjDetail get(int index) {
        return data.get(index);
    }

    public void add(AdjDetail product) {
        data.add(product);
        fireTableRowsInserted(getRowCount() - 1, getRowCount() - 1);
    }

    public void remove(int index) {
        data.remove(index);
        fireTableRowsDeleted(index, index);
    }

    public void set(int index, AdjDetail product) {
        data.set(index, product);
        fireTableRowsUpdated(index, index);
    }

    public void clear() {
        data.clear();
        fireTableDataChanged();
    }

    public void add(Collection<AdjDetail> products) {
        data.addAll(products);
        fireTableRowsInserted(getRowCount() - (products.size()), getRowCount() - 1);
    }
    
    @Override
    public int getRowCount() {
        return data.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    
    @Override
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
        switch (columnIndex) {
//            case 4: 
//                BigDecimal qtystok = TextComponentUtils.parseNumberToBigDecimal((String)value);
//                data.get(rowIndex).setQtyStok(qtystok);
//                fireTableCellUpdated(rowIndex, columnIndex);
//                break;
                
            case 5: 
                BigDecimal qtyreal = TextComponentUtils.parseNumberToBigDecimal((String)value);
                data.get(rowIndex).setQtyReal(qtyreal);
                fireTableCellUpdated(rowIndex, columnIndex);
                break;                            
//            case 6: 
//                BigDecimal qtyrange = TextComponentUtils.parseNumberToBigDecimal((String)value);
//                data.get(rowIndex).setQtyRange(qtyrange);
//                fireTableCellUpdated(rowIndex, columnIndex);
//                break;                            
//            case 7: 
//                BigDecimal price = TextComponentUtils.parseNumberToBigDecimal((String)value);
//                data.get(rowIndex).setPrice(price);
//                fireTableCellUpdated(rowIndex, columnIndex);
//                break;                            
//            case 8: 
//                BigDecimal amountreal = TextComponentUtils.parseNumberToBigDecimal((String)value);
//                data.get(rowIndex).setAmountReal(amountreal);
//                fireTableCellUpdated(rowIndex, columnIndex);
//                break;                            
//            case 9: 
//                BigDecimal amountrange = TextComponentUtils.parseNumberToBigDecimal((String)value);
//                data.get(rowIndex).setAmountRange(amountrange);
//                fireTableCellUpdated(rowIndex, columnIndex);
//                break;                            
                
                 
        }
    }
    
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case 0: return rowIndex+1;
            case 1: return data.get(rowIndex).getItem().getPlu();
            case 2: return data.get(rowIndex).getItem().getNama();
            case 3: return data.get(rowIndex).getItem().getUnit().getNama();
            case 4: return data.get(rowIndex).getQtyStok();
            case 5: 
                   String qtyPcsDisplay = TextComponentUtils.formatNumber(data.get(rowIndex).getQtyReal());
                    return qtyPcsDisplay; 
                    
            case 6: return data.get(rowIndex).getQtyRange();
            case 7: return data.get(rowIndex).getPrice();
            
            case 8: return data.get(rowIndex).getAmountReal();
            case 9: return data.get(rowIndex).getAmountRange();
            case 10: return data.get(rowIndex).getId().getItemId();
            default: return null;
        }
    }
    
     @Override
    public boolean isCellEditable(int row, int column) {
        return (column==1 || column==5)&& (readOnly==false);
    }
     
    @Override
    public String getColumnName(int column) {
       return columnNames[column];
    }
    
    @Override
    public Class getColumnClass(int c) {
       //  return columnTypes[c];
            //return getValueAt(0, c).getClass();
        Object value=this.getValueAt(0,c);  
        return (value==null?Object.class:value.getClass());  
    }
}
